package swapper.view.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import swapper.facade.PropostaFacade;
import org.springframework.web.servlet.mvc.SimpleFormController;
import javax.servlet.ServletException;
import org.springframework.web.servlet.view.RedirectView;
import org.springframework.web.bind.ServletRequestDataBinder;
import swapper.facade.StatusFacade;
import swapper.model.bean.Proposta;
import swapper.model.bean.Status;

/**
 *
 * @author Valentino
 */
public class GerenciarPropostasController extends AbstractSimpleForm {

    public GerenciarPropostasController(){
        super("proposta", Proposta.class);
    }
    
    private PropostaFacade propostafacade;
    private StatusFacade statusfacade;

    public void setPropostaFacade(PropostaFacade propostafacade) {
        this.propostafacade = propostafacade;
    }

    public void setStatusFacade(StatusFacade statusfacade) {
        this.statusfacade = statusfacade;
    }

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest hsr,HttpServletResponse hsr1) throws Exception {
        //Atualiza sessao
        isLogado(hsr);

        ModelAndView modelAndView =new ModelAndView("gerenciarpropostas");

        modelAndView.addObject("produtos", propostafacade.listAll());
        modelAndView.addObject("logado", isLogado(hsr));
        modelAndView.addObject("pessoaSessao", getPessoaSession(hsr));


        return modelAndView;

    }

    @Override
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
        //Atualiza sessao
        isLogado(request);
        

        //Proposta proposta = (Proposta) binder.getTarget();

        //proposta.setStatus((Status) statusfacade.findByCod(Long.parseLong("2")));
        
        
        
        Long PropostaCodigo = null;
        try {
            
            PropostaCodigo = Long.parseLong(request.getParameter("codigo"));
        } catch (Exception e) {
        }
        if (PropostaCodigo != null) {
            
            Proposta proposta =(Proposta) propostafacade.findByCod(PropostaCodigo);

            proposta.setStatus((Status)statusfacade.findByCod(Long.parseLong("2")));
            
            propostafacade.update(proposta);

        }
        
        

        /*
        Long EstadoProdutocodigo = null;
        try {
        EstadoProdutocodigo = Long.parseLong(request.getParameter("estadoproduto.codigo"));
        } catch (Exception e) {
        }
        if (EstadoProdutocodigo != null) {
        
        produto.setEstadoproduto((EstadoProduto) estadoprodutofacade.findByCod(EstadoProdutocodigo));
        
        }
         */

    }

    @Override
    public ModelAndView onSubmit(Object command) throws ServletException {
        ModelAndView modelAndView = new ModelAndView("gerenciarpropostas");
        modelAndView.addObject("logado", isLogado());
        modelAndView.addObject("pessoaSessao", getPessoaSession());
        ((Proposta) command).setStatus((Status) statusfacade.findByCod(Long.parseLong("2")));

        propostafacade.update((Proposta) command);

        System.out.println("##########################################################################################3");




        return modelAndView;
    }
    
    @Override
    protected Object formBackingObject(HttpServletRequest request) throws Exception {
        return null;
      }
}
